/**
 * Created by leo on 2/2.
 */
angular.module('panels')
	.directive('panelItilTwolineTodoCount', function ($http, tabService, LoginInfo, loadConf, $timeout, $log, Promise) {
		return {
			link: function (scope) {
				Promise.all({
					setting: loadConf('panel-itil-twoline-todo-count.yml'),
					loginInfo: LoginInfo
				}).then(function (result) {
					var setting = result.setting, loginInfo = result.loginInfo, timeoutId;
					scope.more = setting.more;
					if (setting.processList) {
						scope.todoList = setting.processList;
					}
					scope.$on('$destroy', function () {
						timeoutId && $timeout.cancel(timeoutId);
					});
					(function updateData() {
						$http.jsonp(setting.listQueryUrl.replace('{userId}', loginInfo.currentUser.LOGIN_ID)).success(function (data) {
							scope.rows = data;
						}).then(callback, callback);
						function callback() {
							if (setting.updateInterval > 0) {
								timeoutId = $timeout(updateData, setting.updateInterval * 1000);
							}
						}

						$log.debug('panel-update data: itil-to-do-task-list-personal');
					}());


					scope.openSubView = function (groupId,isColor,type){
						var url =setting.toDoListUrl.replace('{GROUPID}', groupId).replace('{ISCOLOR}',isColor).replace('{TYPE}',type);
						tabService.addTab({
							name : "二线投诉工单待处理列表",
							url :url
						});
					};
					scope.openSubViewTodayCount = function (groupId,isColor){
						var url =setting.todayCountListUrl.replace('{GROUPID}', groupId).replace('{ISCOLOR}',isColor);
						tabService.addTab({
							name : "二线投诉工单今日派单量列表",
							url :url
						});
						//window.open(url);
					};
					scope.openSubViewDeal = function (groupId,isColor,type){
						var url =setting.dealListUrl.replace('{GROUPID}', groupId).replace('{ISCOLOR}',isColor).replace('{TYPE}',type);
						if(type=="0")
						{
							tabService.addTab({
								name : "二线投诉工单本月已超时列表",
								url :url
							});
						}
						else
						{
							tabService.addTab({
								name : "二线投诉工单当月处理列表",
								url :url
							});
						}
					};
					scope.openSubViewReturnCount = function (groupId,isColor){
						var url =setting.returnCountListUrl.replace('{GROUPID}', groupId).replace('{ISCOLOR}',isColor);
						tabService.addTab({
							name : "二线投诉工单本月返单量列表",
							url :url
						});
					};
					scope.doViewHistory = function()
					{
						var url =setting.historyCountUrl;
						tabService.addTab({
							name : "二线投诉历史信息",
							url :url
						});
					}

				});
			},
			templateUrl: 'app/panel/itil/twoline-todo-count.html'
		};
	});